package com.chinasoft.mall.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import android.content.Context;
import android.content.SharedPreferences;

import com.chinasoft.mall.model.response.GoodsByClassBean;

/**
 * 将数据保存到本地
 * 
 * @author Administrator
 * 
 */
public class LocalDataCommend {
	private static String USERID = "local";

	public static void addGoodsSelect(Context mContext, String goodid) {
		SharedPreferences settings = mContext.getSharedPreferences(USERID,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		String _goodsids = settings.getString("goodsids", "");
		if (_goodsids.endsWith("@")) {
			_goodsids = (new StringBuffer(_goodsids)).append(goodid)
					.append("@").toString();
		} else {
			_goodsids = (new StringBuffer(_goodsids)).append("@")
					.append(goodid).append("@").toString();
		}
		editor.putString("goodsids", _goodsids);
		editor.commit();
	}

	public static void removeGoodsSelect(Context mContext, String goodid) {
		SharedPreferences settings = mContext.getSharedPreferences(USERID,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		String strIds = settings.getString("goodsids", "");
		editor.putString("goodsids", strIds.replaceAll("@" + goodid + "@", "@"));
		editor.commit();
	}

	public static List<String> getGoodsSelect(Context mContext) {
		SharedPreferences settings = mContext.getSharedPreferences(USERID,
				Context.MODE_PRIVATE);
		String gs = settings.getString("goodsids", "");
		return new ArrayList<String>(Arrays.asList(gs.split("@")));
	}

	/**
	 * 获得所有商品
	 * 
	 * @param mContext
	 * @return
	 */
	public static List<GoodsByClassBean> getGoodsListBean(Context mContext) {
		SharedPreferences settings = mContext.getSharedPreferences(USERID,
				Context.MODE_PRIVATE);
		int size = settings.getInt("GoodsByClassBeanSize", 0);
		List<GoodsByClassBean> gbcb = new ArrayList<GoodsByClassBean>(size);
		for (int i = 0; i < size; i++) {
			GoodsByClassBean _gb = new GoodsByClassBean();
			_gb.setGOODS_ID(settings.getString("GOODS_ID" + i, null));
			_gb.setCLASS_ID(settings.getString("CLASS_ID" + i, null));
			_gb.setGOODS_NAME(settings.getString("GOODS_NAME" + i, null));
			_gb.setPRC(settings.getString("PRC" + i, null));
			_gb.setMPRC(settings.getString("MPRC" + i, null));
			_gb.setTHUM(settings.getString("THUM" + i, null));
			_gb.setPUB_DATE(settings.getString("PUB_DATE" + i, null));
			_gb.setValid(settings.getString("valid" + i, null));
			gbcb.add(_gb);
		}
		return gbcb;
	}

	/**
	 * 获得所有商品个数
	 * 
	 * @param mContext
	 * @return
	 */
	public static int getGoodsListSize(Context mContext) {
		SharedPreferences settings = mContext.getSharedPreferences(USERID,
				Context.MODE_PRIVATE);
		return settings.getInt("GoodsByClassBeanSize", 0);
	}

	/**
	 * 增加一个商品数据
	 * 
	 * @param mContext
	 * @param _gb
	 * @return ture 收藏成功 false 已经收藏
	 */
	public static boolean addGoodsBean(Context mContext, GoodsByClassBean _gb) {
		if (getGoodsListBean(mContext).contains(_gb)) {
			return false;
		}
		SharedPreferences settings = mContext.getSharedPreferences(USERID,
				Context.MODE_PRIVATE);
		int oldsize = settings.getInt("GoodsByClassBeanSize", 0);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString("GOODS_ID" + oldsize, _gb.getGOODS_ID());
		editor.putString("CLASS_ID" + oldsize, _gb.getCLASS_ID());
		editor.putString("GOODS_NAME" + oldsize, _gb.getGOODS_NAME());
		editor.putString("PRC" + oldsize, _gb.getPRC());
		editor.putString("MPRC" + oldsize, _gb.getMPRC());
		editor.putString("THUM" + oldsize, _gb.getTHUM());
		editor.putString("PUB_DATE" + oldsize, _gb.getPUB_DATE());
		editor.putString("valid" + oldsize, _gb.getValid());
		editor.putInt("GoodsByClassBeanSize", oldsize + 1);
		editor.commit();
		return true;
	}

	/**
	 * 删除一个商品数据
	 * 
	 * @param mContext
	 * @param gbcb
	 */
	public static void removeGoodsBean(Context mContext, GoodsByClassBean _gb) {
		List<GoodsByClassBean> gbs = getGoodsListBean(mContext);
		gbs.remove(_gb);
		replaceGoodsBean(mContext, gbs);
	}

	/**
	 * 删除一组商品数据
	 * 
	 * @param mContext
	 * @param gbcb
	 */
	public static void removeGoodsListBean(Context mContext,
			List<GoodsByClassBean> _gb) {
		List<GoodsByClassBean> gbs = getGoodsListBean(mContext);
		gbs.removeAll(_gb);
		replaceGoodsBean(mContext, gbs);
	}

	/**
	 * 保存收藏数组 替换原数据
	 * 
	 * @param mContext
	 * @param gbcb
	 */
	public static void replaceGoodsBean(Context mContext,
			List<GoodsByClassBean> gbcb) {
		SharedPreferences settings = mContext.getSharedPreferences(USERID,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		int gbsize = gbcb.size();
		for (int i = 0; i < gbsize; i++) {
			GoodsByClassBean _gb = gbcb.get(i);
			editor.putString("GOODS_ID" + i, _gb.getGOODS_ID());
			editor.putString("CLASS_ID" + i, _gb.getCLASS_ID());
			editor.putString("GOODS_NAME" + i, _gb.getGOODS_NAME());
			editor.putString("PRC" + i, _gb.getPRC());
			editor.putString("MPRC" + i, _gb.getMPRC());
			editor.putString("THUM" + i, _gb.getTHUM());
			editor.putString("PUB_DATE" + i, _gb.getPUB_DATE());
			editor.putString("valid" + i, _gb.getValid());
		}
		editor.putInt("GoodsByClassBeanSize", gbsize);
		editor.commit();
	}

	/**
	 * 保存String到key里
	 * 
	 * @param mContext
	 */
	public static void saveOriginalString(Context mContext, String key,
			String value) {
		SharedPreferences settings = mContext.getSharedPreferences(USERID,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putString(key, value);
		editor.commit();
	}

	/**
	 * 保存boolean到key里
	 * 
	 * @param mContext
	 */
	public static void saveOriginalBoolean(Context mContext, String key,
			boolean value) {

		SharedPreferences settings = mContext.getSharedPreferences(USERID,
				Context.MODE_PRIVATE);
		SharedPreferences.Editor editor = settings.edit();
		editor.putBoolean(key, value);
		editor.commit();
	}

	/**
	 * 从key中获取value
	 * 
	 * @param mContext
	 * @return
	 */
	public static String getOriginalString(Context mContext, String key,
			String def) {
		SharedPreferences settings = mContext.getSharedPreferences(USERID,
				Context.MODE_PRIVATE);
		return settings.getString(key, def);
	}

	/**
	 * 从key中获取value
	 * 
	 * @param mContext
	 * @return
	 */
	public static boolean getOriginalBoolean(Context mContext, String key,
			Boolean def) {
		SharedPreferences settings = mContext.getSharedPreferences(USERID,
				Context.MODE_PRIVATE);
		return settings.getBoolean(key, def);
	}
}